/*
 *    Filename:	FreezeControl.m 
 *    Created :	Sat Dec 21 00:24:10 1991 
 *    Author  :	Vince DeMarco
 *		<vince@whatnxt.cuc.ab.ca>
 */


/* Generated by Interface Builder */

#import <appkit/Application.h>
#import <appkit/OpenPanel.h>
#import <appkit/Panel.h>

#import <appkit/Matrix.h>
#import <appkit/Menu.h>
#import <appkit/MenuCell.h>

#import <appkit/Pasteboard.h>
#import <appkit/Listener.h>

#import <mach.h>
#import <mach_interface.h>

#import <strings.h>
#import "FreezeControl.h"
#import "ProcessControl.h"
#import "isdir.h"

@implementation FreezeControl

- init
{
    self = [super init];
    openPanel = [OpenPanel new];
    [openPanel setTitle:"Process File"];
    [openPanel allowMultipleFiles:YES];
    return self;
}

- free
{
    [openPanel free];
    return [super free];
}

- processFile:sender
{
    int i=0;
    char pathname[1024];
    const char *directory;

    if ([openPanel runModal]){
	while ([openPanel filenames][i]){
	    directory = [openPanel directory];
	    sprintf(pathname,"%s/%s",directory,[openPanel filenames][i++]);
	    if (isdir(pathname)){
		[processControl addDirectory:pathname];
	    }else{
		[processControl addFile:pathname];
	    }
	}
    }
    return self;
}

- appDidInit:sender
{
    /* Set up services stuff */
    [[NXApp appListener] setServicesDelegate:self];
    return self;
}

- appWillTerminate:sender
{
    int alert_return = 1;

    /* Check if buffer is actually empty before exiting */
    if ([processControl backgroundjobs]) {
	alert_return = NXRunAlertPanel("Quit",
				       "Background processes active, Quit anyways?",
				       "Quit","Cancel",NULL);
	switch (alert_return){
	    case NX_ALERTDEFAULT  :
		[processControl killBackground:sender];
		return self;
	    case NX_ALERTALTERNATE:
	    case NX_ALERTOTHER    :
	    case NX_ALERTERROR    : 
		break;
	    }
	return nil;  /* Return nil to tell Application Object that it shouldn't quit */
    }else{
	return self; /* Return non-nill if it is okay to quit */
    }
}

- (int)appOpenFile:(const char *)path type:(const char *)type
{
    [processControl addFile:(char *)path];    
    return YES;
}

- (BOOL)appAcceptsAnotherFile:sender
{
    return YES;
}

/* Services stuff */
- processDocument:myPasteboard userData:(const char*)userData error:(char**)message
{
    char         *data  = NULL;
    int           length= 0;
    char         *temp  = NULL;
    char         *temp2 = NULL;

    const NXAtom *myTypes;
    

    myTypes = [myPasteboard types]; // You need this for some stupid idiotic reason

    if ([myPasteboard readType:NXFilenamePboardType data:&data length:&length]){
	if (length && data){
	    data[length]= '\000';
	    if ((temp = index(data,'\t')) == NULL){  /* Check if multiple files selected */
		if (isdir(data)){
		    [processControl addDirectory:data];
		}else{
		    [processControl addFile:data];
		}        
	    }else{
		temp2 = data;
		while(temp != NULL){
		    *temp = '\000';
		    temp = temp+1;
		    if (isdir(temp2)){
			[processControl addDirectory:temp2];
		    }else{
			[processControl addFile:temp2];
		    }
		    temp2 = temp;
		    temp = index(temp2,'\t');
		}
		if (isdir(temp2)){
		    [processControl addDirectory:temp2];
		}else{
		    [processControl addFile:temp2];
		}
	    }
	    vm_deallocate(task_self(),(vm_address_t)data,(vm_size_t)length);
	}else{
	    *message = "Selection is empty.";
	}
    }
    return(self);
}

@end
